package clientGUI;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import college.RequestTreatment;
import college.User;
import college.User.UserTypes;

import clientGUI.generalPanels.*;
import clientGUI.user.ISD.*;
import clientGUI.userPanels.*;
import clientserver.Client;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CollegeClientGUI extends JFrame {



	private JPanel contentPane = null;
	private ClientInit initpanel = null;
	private ClientLogin loginpanel = null;
	private ChangeRequestPanel reqpanel = null;
	private InitiatorPanel initiatorpanel = null;
	private ISEGatePanel  isePanel=null;
	private  InitiatorApproveClosure inc = null;
	private UPanel initiatormainpanel =null;

	private int currentrid;
	private ISDChoose isdtabs = null;
	private CollegeClientGUI frame;
	private boolean loggedin; /// true if user logged in server
	private User user;			//  if loggedin true than  here logged in user,
	private Client connectedclient;
	

	public void alert(String msg) {
		JOptionPane.showMessageDialog(this, msg);
	}

	/******************** Panel function section *********************/
	public void setLoginPanel() {
		loggedin=false;
		user=null;
		loginpanel = new ClientLogin();
		loginpanel.SetFrame(this);
		JPanel contentPane = (JPanel) getContentPane();

		contentPane.removeAll();
		contentPane.add(loginpanel);
		contentPane.revalidate();
		contentPane.repaint();
		connectedclient= initpanel.getClient();
		loginpanel.setClient(connectedclient);
		setContentPane(loginpanel);

	}

	public void setChangeRequestPanel() {
		 System.out.println("set change request");
		reqpanel = new ChangeRequestPanel(connectedclient);
		reqpanel.SetFrame(this);
		// Init requestpanel variables:
		String login = loginpanel.getLoggeduser();
			reqpanel.setInitiator(login);

		// panel swap:
		JPanel contentPane = (JPanel) getContentPane();
		contentPane.removeAll();
		contentPane.add(reqpanel);
		contentPane.revalidate();
		contentPane.repaint();
		setContentPane(reqpanel);

	}

	
	
/**Make user logout from database and also change panel to log in panel 
 * 
 */
	public void logout()
	{
		loginpanel.logout();
	setLoginPanel();
		
	}
	

	/*
	 * swap panel or panel with tabs in the frame also gives ability to send
	 * parameter to panel constructor, need's casting before constructor run
	 * 
	 * @param type - using this enum this function decides which panel to show
	 * and how
	 * 
	 * @param parameter - parameter which we will send to the Panel constructor,
	 * parameter=null if no parameters needed
	 */


	public void SetPanel( UPanel panel)
	
	{
		
		UPanel contentPane = (UPanel) getContentPane();
		panel.SetFrame(this);
		contentPane.removeAll();
		contentPane.add(panel);
		contentPane.revalidate();
		contentPane.validate();
		contentPane.repaint();
		setContentPane(panel);
		
	}
	

	public void setUserPanel(User.UserTypes type) {
		UPanel panel = null;
		
		switch (type) // choose type of panel to set by use type
		{
		case Lector:
		case Student: initiatorpanel = new InitiatorPanel(connectedclient, user);
		panel=initiatorpanel;
		break;
			
			
			
			
		case ISE:  isePanel=new ISEGatePanel(connectedclient);	
		isePanel.SetFrame(this);
		panel=isePanel;	
		break;

		}
	
		
		

		// panel swap:
		UPanel contentPane = (UPanel) getContentPane();
		
		if(type==UserTypes.ISD)
		{
		connectedclient=initpanel.getClient();
			isdtabs = new ISDChoose(connectedclient);
			contentPane.removeAll();
			contentPane.add(isdtabs);
			contentPane.revalidate();
			contentPane.validate();
			contentPane.repaint();
			setContentPane(isdtabs);	
		}
		else
		{
			panel.SetFrame(this);
		contentPane.removeAll();
		contentPane.add(panel);
		contentPane.revalidate();
		contentPane.validate();
		contentPane.repaint();
		setContentPane(panel);
		
		}
		
	}


	public void setInitiatorPanel() 
	{
		InitiatorPanel c;
		c= new InitiatorPanel(connectedclient,user);
		c.SetFrame(this);
		JPanel contentPane = (JPanel) getContentPane();
		contentPane.removeAll();
		contentPane.add(c);
		contentPane.revalidate();
		contentPane.repaint();
		connectedclient= initpanel.getClient();
		c.setClient(connectedclient);
		setContentPane(c);

	}

	
	

	/*
	 * if user is set up by login panel in this class, than this function uses
	 * SetUserPanel to showMain User Panel by user types
	 */
	public void SetMainUserPanel()

	{
		
	
		User.UserTypes type = user.getType();
		setUserPanel(type);
	}

	/*
	 * ///////////////////////////////////End panel
	 * section////////////////////////////////
	 */

	// --------------getters and
	// setters-------------------------------------------

	/*
	 * return curent logged in user, must be called only after islogged(), if no
	 * one is logged returns null
	 */
	public User getUserlogged() {

		if (loggedin)
			return user;
		return null;
	}

	// set current user logged in GUI
	public void setUserlogged(User userlogged) {
		loggedin = true;
		this.user = userlogged;

	}

	/*
	 * Returns true if user is logged in GUI, if not : false
	 */
	public boolean islogged() {
		return loggedin;
	}

	/*
	 * set current request id for treatment
	 */
	public void setCurrentRequestId(int id) {
		currentrid = id;
	}

	
	
	public void setInitPanel() {
		initpanel = new ClientInit();
		initpanel.SetFrame(this);
		JPanel contentPane = (JPanel) getContentPane();

		contentPane.removeAll();
		contentPane.add(initpanel);
		contentPane.revalidate();
		contentPane.repaint();
		setContentPane(initpanel);

	}

	
	
	
	// -------------------------------------------------------------------------------

	public static void main(String[] args)

	{

		CollegeClientGUI aFrame = new CollegeClientGUI();

		aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		aFrame.setSize(defines.window_width, defines.window_height);
		aFrame.setVisible(true);
	}

	public CollegeClientGUI() {

		super();
		addWindowListener(new WindowAdapter() {
		
			public void windowClosing(WindowEvent e) {
		
			if(loggedin) // than log out
			{
				
				loginpanel.logout();
			}
				
				
			}
		});
		

		setTitle("Information System Manager");
		setIconImage(Toolkit.getDefaultToolkit().getImage(CollegeClientGUI.class.getResource("/com/sun/java/swing/plaf/windows/icons/Inform.gif")));
		setBounds(100, 100, defines.window_width, defines.window_height);

		// create  init panel
		initpanel = new ClientInit();
		MainPanel mainp = new MainPanel("ims.jpg");
	mainp.SetFrame(this);
		
		
		
		

	
		

		// set first client panel
		setContentPane(mainp);

	}

}
